package com.example.image.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.image.R;
import com.example.image.model.FolderItem;
import com.example.image.util.ImageUtil;

public class AdapterFolder extends BaseAdapter {
	private Context context;
	private ArrayList<FolderItem> list;
	private int width;

	/**
	 * @param context
	 * @param width
	 *            屏幕宽度
	 */
	public AdapterFolder(Context context, int width) {
		this.context = context;
		list = new ArrayList<FolderItem>();
		this.width = width / 3;// 每个文件显示的宽度为屏幕宽度的1/4
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public FolderItem getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		FolderItem item = getItem(position);
		view = LayoutInflater.from(context).inflate(R.layout.folder_item, null);
		LinearLayout llFodler = (LinearLayout) view
				.findViewById(R.id.ll_folder_item);
		TextView tvName = (TextView) view.findViewById(R.id.tv_folder_name);
		TextView tvCount = (TextView) view.findViewById(R.id.tv_folder_count);
		llFodler.setLayoutParams(new LayoutParams(width, width));
		tvName.setText(item.getName());
		tvCount.setText("数量:" + item.getCount());
//		llFodler.setBackgroundDrawable(ImageUtil.getDrawable(item
//				.getImagePaths().get(0), width)); // 设置文件夹内第一张图片为缩略图
		return view;
	}

	public void add(FolderItem[] values) {
		for (FolderItem folderItem : values) {
			list.add(folderItem);
		}
		notifyDataSetChanged();
	}

}
